package client_controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import client_gui.AnaliticSystemGUI;
import client_gui.CampaignPatternGUI;
import client_gui.MarketingEmployeeGUI;
//<<<<<<< .mine
import client_gui.ProductGUI;
//=======
import client_gui.MarketingManagerGUI;
//>>>>>>> .r45
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;

/**
 * @class MarketingEmployeeController Allows switching between screens of the markting employee by 
 * 
 * @param MarketingEmployeeGUI links to GUI.
 * @param EmployeeLoginController checks for employee authorization.
 * @param MarketingEmployeeController links to DB & marketing employee list.
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 */

public class MarketingEmployeeController implements dbHandler {
	private MarketingEmployeeGUI meg;
	private EmployeeLoginController elc;
	private Boss b;
	private MarketingEmployeeController mec;
	
	public MarketingEmployeeController(MarketingEmployeeGUI marketingEmployeeGUI,EmployeeLoginController elc) {
		mec = this;
		this.elc= elc;
		this.meg= marketingEmployeeGUI;
		meg.setCampaignPatternLis(new CampaignPatternListener());
		meg.setMaintainProductLis(new MaintainProductListener());
		meg.setAnalysisSystemLis(new AnalysisSystemListener());
		meg.setLogoutLis(new LogoutListener());
		meg.setexittlis(new Exit());

	}
	
	public MarketingEmployeeController(MarketingManagerGUI mmg,EmployeeLoginController elc) {
		this.elc= elc;
		mmg.setCampaignPatternLis(new CampaignPatternListener());
		mmg.setMaintainProductLis(new MaintainProductListener());
		mmg.setAnalysisSystemLis(new AnalysisSystemListener());
		mmg.setLogoutLis(new LogoutListener());
		mmg.setexittlis(new Exit());

	}
	
	
	public class CampaignPatternListener implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			//open new campaign pattern gui with param - this , elc 
			
	
			meg.setVisible(false);	
			CampaignPatternGUI cpg = new CampaignPatternGUI(mec,elc);  //is it correctp  yes run

		}
	}
	
	public class MaintainProductListener implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			//open new maintain product gui with param - this , elc 
			meg.setVisible(false);	
			ProductGUI pg = new ProductGUI(mec,elc);  //is it correctp  yes run
		}
	}
	
	public class AnalysisSystemListener implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			meg.setVisible(false);	
			AnaliticSystemGUI asg = new AnaliticSystemGUI(mec,elc);
		}
	}
	public class LogoutListener implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			//elc should disconnect us from the server - and update database
    		setB(new Boss(elc.getEmp(),"logout","MarketingEmployeeController"));
    		update();
		}


	}
	public class Exit implements WindowListener{

		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosed(WindowEvent e) {
			
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
    		setB(new Boss(elc.getEmp(),"logout","MarketingEmployeeController"));
    		update();
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	

	@Override
	public void Add() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Search() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void HandleMsg(Object msg) {
		this.setB((Boss) msg);
		if(this.getB().getMsg().equals("logout")){
			this.getMeg().dispose();
			this.getElc().getElg().setVisible(true);
		}
		
		
	}

	@Override
	public void update() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public Boss getB() {
		return b;
	}

	public void setB(Boss b) {
		this.b = b;
	}
	public MarketingEmployeeGUI getMeg() {
		return meg;
	}

	public void setMeg(MarketingEmployeeGUI meg) {
		this.meg = meg;
	}
	public EmployeeLoginController getElc() {
		return elc;
	}

	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}

}
